###
Max function
###
max = (x, y) -> if x < y then x else y

###
Min function
###
min = (x, y) -> if x > y then x else y

###
Check variable defined or not

@param {type} v
@returns {Boolean}
###
isset = (v) ->
    (if (typeof v is "undefined") then true else false)

###
Send an Ajax request

@param action action name
@param data object data
@param func callback function
@param secure security mode
@example send('getwidget', {key1:'value1', key2:'value2'}, function(result) { your code }, false);
@default secure = true
###
send = (action, data, func, secure) ->
    secure = secure or false
    syn = app.root + "/syn/"
    if secure
        data.uid = app.uid
        data.token = app.token
    url = syn + action.toLowerCase()
    $.ajax(
        url: url
        type: "POST"
        async: false
        data: data
    ).success (result) ->
        func result


###
Render HTML template

@param template id of element
@param data object
@example <div class="template" id="abc"><div class="{key1}">{key2}</div></div>
@example render('abc', {key1:'name', key2:'Hello My name is Loi'});
###
render = (template, data) ->
    html = $("#" + template).html()
    pattern = /\{[\sa-z0-9_]+\}/g
    result = ""
    index = 0
    var_ = undefined
    while match = pattern.exec(html)
        var_ = match[0].substring(1, match[0].length - 1).trim()
        result += html.substring(index, match.index)
        eval "result+=data." + var_
        index = pattern.lastIndex
    result += html.substring(index, html.length)
    result